package com.dou.yygh.hosp.controller.api;

import com.dou.yygh.hosp.service.ScheduleService;
import com.dou.yygh.model.hosp.Schedule;
import com.dou.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.data.domain.Page;
import com.dou.yygh.common.exception.YyghException;
import com.dou.yygh.common.helper.HttpRequestHelper;
import com.dou.yygh.common.result.Result;
import com.dou.yygh.common.result.ResultCodeEnum;
import com.dou.yygh.hosp.service.DepartmentService;
import com.dou.yygh.hosp.service.HospitalService;
import com.dou.yygh.hosp.service.HospitalSetService;
import com.dou.yygh.common.util.MD5;
import com.dou.yygh.model.hosp.Department;
import com.dou.yygh.model.hosp.Hospital;
import com.dou.yygh.vo.hosp.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA 2021.1 x64
 *
 * @Author: 窦义超
 * @Date: 2021/08/2021/8/26/11:44
 * @Description:
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
        @Autowired
        private HospitalSetService hospitalSetService;
        @Autowired
        private  HospitalService hospitalService;
        @Autowired
        private DepartmentService departmentService;
        @Autowired
        private ScheduleService scheduleService;
        //删除排班接口
        @PostMapping("schedule/remove")
        public Result remove(HttpServletRequest request){
            //获取传递过来的医院信息
            Map<String, String[]> requestMap = request.getParameterMap();
            Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

            //获取医院系统传递过来的签名,签名进行了md5的加密
            String  hospSign = (String) paramMap.get("sign");


            //医院编号  和科室编号
            String hoscode = (String) paramMap.get("hoscode");
            String depcode = (String) paramMap.get("depcode");
            String hosScheduleId = (String) paramMap.get("hosScheduleId");
            String signKey = hospitalSetService.getSignKey(hoscode);
            //把数据库查询签名进行md5加密
            String encrypt = MD5.encrypt(signKey);
            //判断签名是否一致
            if(!hospSign.equals(encrypt)){
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
            scheduleService.remove(hoscode,hosScheduleId);
            return Result.ok();
        }
        //查询排班接口
        @PostMapping("schedule/list")
        public Result findSchedule(HttpServletRequest request){
            //获取传递过来的医院信息
            Map<String, String[]> requestMap = request.getParameterMap();
            Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
            //获取医院编号

            //获取医院系统传递过来的签名,签名进行了md5的加密
            String  hospSign = (String) paramMap.get("sign");

            //医院编号
            String hoscode = (String) paramMap.get("hoscode");
            //科室编号
            String depcode = (String) paramMap.get("depcode");
            int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
            int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
            String signKey = hospitalSetService.getSignKey(hoscode);
            //把数据库查询签名进行md5加密
            String encrypt = MD5.encrypt(signKey);
            //判断签名是否一致
            if(!hospSign.equals(encrypt)){
                throw new YyghException(ResultCodeEnum.SIGN_ERROR);
            }
            ScheduleQueryVo scheduleQueryVo=new ScheduleQueryVo();
            scheduleQueryVo.setHoscode(hoscode);
            scheduleQueryVo.setDepcode(depcode);
            Page<Schedule> page1= scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
            return Result.ok(page1);
        }
    //上传排班接口
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院系统传递过来的签名,签名进行了md5的加密
        String  hospSign = (String) paramMap.get("sign");


        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        scheduleService.save(paramMap);
        return Result.ok();
    }
    //删除科室接口
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //获取医院系统传递过来的签名,签名进行了md5的加密
        String  hospSign = (String) paramMap.get("sign");


        //医院编号  和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.remove(hoscode,depcode);
        return Result.ok();
    }

    //查询科室接口
    @PostMapping("department/list")
        public Result findDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院编号

        //获取医院系统传递过来的签名,签名进行了md5的加密
        String  hospSign = (String) paramMap.get("sign");

        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        int page = StringUtils.isEmpty(paramMap.get("page"))?1:Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit"))?1:Integer.parseInt((String) paramMap.get("limit"));
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        DepartmentQueryVo departmentQueryVo=new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
      Page<Department> page1= departmentService.findPageDepartment(page,limit,departmentQueryVo);
        return Result.ok(page1);
    }

    //上传科室接口
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院系统传递过来的签名,签名进行了md5的加密
        String  hospSign = (String) paramMap.get("sign");


        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(encrypt)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service方法
        departmentService.save(paramMap);
        return Result.ok();
    }
    //上传医院接口
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院系统传递过来的签名,签名进行了md5的加密
        String  hospSign = (String) paramMap.get("sign");


        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
        //判断签名是否一致
        if(!hospSign.equals(encrypt)){
            System.out.println(hospSign);
            System.out.println(signKey);
            System.out.println(hoscode);
            System.out.println(encrypt);
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //传输过程中"+" 转换为了"",因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData=logoData.replaceAll(" ","+");
        paramMap.put("logoData",logoData);
        //调用service的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院系统传递过来的签名,签名进行了md5的加密
        String  hospSign = (String) paramMap.get("sign");


        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //把数据库查询签名进行md5加密
        String encrypt = MD5.encrypt(signKey);
    if(!hospSign.equals(encrypt)){
            System.out.println(hospSign);
            System.out.println(signKey);
            System.out.println(hoscode);
            System.out.println(encrypt);
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

     Hospital hospital= hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }
}
